/**
 * 
 */
package org.estela.evaluation;

import java.io.Serializable;
import java.util.Arrays;

/**
 * 
 * 
 * @version $Revision$
 * @date Feb 26, 2006
 * @author <a href="mailto:akantek@nyse.com">Antonio Kantek</a>
 */
public class ConfusionMatrix implements Serializable, Cloneable {
	private static final long serialVersionUID = 1L;
	
	private double[][] _matrix;
	
	/**
	 * 
	 * @param length
	 */
	public ConfusionMatrix(int length) {
		assert length > 0;
		_matrix = new double[length][length];
	}
	
	@Override
	public Object clone(){
		ConfusionMatrix matrix = null;
		try {
			matrix = (ConfusionMatrix) super.clone();
		} catch (CloneNotSupportedException ex) {
			throw new Error(ex);
		}
		matrix._matrix = _matrix.clone();
		for(int i=0;i<_matrix.length;i++) {
			matrix._matrix[i] = _matrix[i].clone();
		}
		return matrix;
	}
	
	@Override
	public String toString() {
		StringBuilder buff = new StringBuilder();
		for(int i=0;i<_matrix.length;i++) {
			buff.append("\n\t").append(Arrays.toString(_matrix[i]));
		}
		return buff.toString();
	}
	
	/**
	 * 
	 * @param i
	 * @param j
	 * @return
	 */
	public double valueAt(int i, int j) {
		return _matrix[i][j];
	}

	/**
	 * 
	 * @param actualClass
	 * @param predictedClass
	 * @param weight
	 */
	public void addValue(int actualClass, int predictedClass, double weight) {
		_matrix[actualClass][predictedClass] += weight;
		
	}

	/**
	 * 
	 * @return
	 */
	public int size() {
		// TODO Auto-generated method stub
		return _matrix.length;
	}

	/**
	 * 
	 * @param i
	 * @param j
	 * @return
	 */
	public double get(int i, int j) {
		// TODO Auto-generated method stub
		return _matrix[i][j];
	}
}